package com.sikaryofficial.backend.service.evaluate.export;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
import com.sikaryofficial.backend.constant.ContentStatusEnum;

import java.text.ParseException;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 数字转字符串
 * @date : 2023/11/29 13:36
 */
public class StatusIntegerStringConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) throws ParseException {
        return NumberUtils.parseInteger(cellData.getStringValue(), contentProperty);
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if (Objects.isNull(value)) {
            return new WriteCellData<>("/");
        }
        return new WriteCellData<>(getStatusDesc(value));
    }

    public String getStatusDesc(Integer status) {
        ContentStatusEnum statusEnum = ContentStatusEnum.getNameByCode(status);
        if (Objects.nonNull(statusEnum)) {
            return statusEnum.getName();
        }
        return "";
    }
}
